﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BHXH.Repository;
using System.Text.RegularExpressions;
using BHXH.LinqRepository;
using DevExpress.XtraGrid.Views.Grid;

namespace BHXH2013.Phieu.PhieuB
{
    public partial class FrmEditPhieuB : FrmEditPhieu
    {

        bool _SavedOK = true;
        bool _NeedPreventInitNewRow = false;
        bool _NeedCheckDataNullInGridView = false;

        EditState _EditState = EditState.Undefined;

        List<BHXH.LinqRepository.Phieu_DangBang> source12;
        BindingSource bind12 = new BindingSource();

        DanhMucPhieu_DangBangRepository _repPhieu_dangBang = new DanhMucPhieu_DangBangRepository();
        KienNghiRepository _repKienNghi = new KienNghiRepository();
        ChiTietPhieuRepository _repChiTietPhieu = new ChiTietPhieuRepository();
        CoQuanRepository _repCoQuan = new CoQuanRepository();
        NguoiTraLoiPVRepository _repNguoiTraloiPV = new NguoiTraLoiPVRepository();
        DanhMucPhieuRepository _repPhieu = new DanhMucPhieuRepository();

        //
        List<BHXH.LinqRepository.Phieu_DangBang> lstSource = new List<BHXH.LinqRepository.Phieu_DangBang>();
        //
        //BHXH.Repository.EditState _EditState = BHXH.Repository.EditState.Undefined;
        BHXH.LinqRepository.Phieu _PhieuA = null;
        BHXH.LinqRepository.NguoiTraLoiPV nguoipv = null;
        BHXH.LinqRepository.NguoiTraLoiPV _NguoiTraLoiPV2 = null;



        public FrmEditPhieuB()
        {
            InitializeComponent();
        }
        public FrmEditPhieuB(BHXH.LinqRepository.Phieu phieuA)
        {
            InitializeComponent();
            if (phieuA != null)
                _EditState = BHXH.Repository.EditState.Modifying;
            else
                _EditState = BHXH.Repository.EditState.CreatingNew;
            _PhieuA = phieuA;
        }

        protected void SetEditState(BHXH.Repository.EditState editState)
        {
            try
            {
                _EditState = editState;

                base.SetEditState(editState);
                switch (editState)
                {
                    case BHXH.Repository.EditState.CreatingNew:
                    case BHXH.Repository.EditState.Modifying:
                        btnSave.Enabled = true;
                        btnEdit.Enabled = false;
                        btnClose.Enabled = true;
                        btnLuu.Enabled = true;
                        btnSua.Enabled = false;
                        btnDong.Enabled = true;

                        btnLuu2.Enabled = true;
                        btnDong2.Enabled = true;
                        btnSua2.Enabled = false;

                        txtPhuongXa.ReadOnly = false;
                        txtSoNha.ReadOnly = false;
                        txtQuanHuyen.ReadOnly = false;
                        txtTinhThanh.ReadOnly = false;
                        break;
                    case BHXH.Repository.EditState.Saved:
                        btnSave.Enabled = false;
                        btnEdit.Enabled = true;
                        btnClose.Enabled = true;
                        btnLuu.Enabled = false;
                        btnSua.Enabled = true;
                        btnDong.Enabled = true;

                        btnLuu2.Enabled = false;
                        btnDong2.Enabled = true;
                        btnSua2.Enabled = true;

                        txtPhuongXa.ReadOnly = true;
                        txtSoNha.ReadOnly = true;
                        txtQuanHuyen.ReadOnly = true;
                        txtTinhThanh.ReadOnly = true;
                        break;
                }
            }
            catch (Exception ex)
            {
            }
        }

        List<BHXH.LinqRepository.Phieu_DangBang> GetSourceByMaCau(int maCau)
        {
            return lstSource.Where(c => c.MaCau == maCau).ToList();
        }

        void LoadCauTraLoiDangBangPhieuA()
        {
            BHXH.LinqRepository.Phieu_DangBang _item = new BHXH.LinqRepository.Phieu_DangBang();
            source12 = new List<BHXH.LinqRepository.Phieu_DangBang>();
            _item = new BHXH.LinqRepository.Phieu_DangBang() { ThuTuTrongBang = 0, STT = "1", ChiTieu = "Tiền lương và các khoản phụ cấp thực lĩnh hàng tháng", Number1 = null };
            source12.Add(_item);
            _item = new BHXH.LinqRepository.Phieu_DangBang() { ThuTuTrongBang = 0, STT = "1", ChiTieu = "Tiền lương, tiền công tháng làm căn cứ đóng BHXH", Number1 = null };
            source12.Add(_item);
            bind12.DataSource = source12;
            gridControlA_12.DataSource = bind12;
            gridControlA_12.RefreshDataSource();
        }
        private void gridViewA_12_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (CountRowInGriView(gridViewA_12) >= 2)
                {
                    _NeedPreventInitNewRow = true;
                }
                else
                {
                    _NeedPreventInitNewRow = false;
                }

                if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab)
                {
                    string columnForcused = gridViewA_12.FocusedColumn.Name;
                    int rowForcused = gridViewA_12.GetFocusedDataSourceRowIndex();
                    if (rowForcused == 1 && columnForcused.Equals("colEndA12"))
                    {
                        if (CheckDataNullInGridView(gridViewA_12, 1)) //nếu không có ô nào để trống thì mới Focus sang control khác
                        {

                            SendKeys.Send("{UP}");
                            nmr13.Focus();

                            bool checkNull = false;
                           foreach(Phieu_DangBang tempPhieu in source12){
                               if (tempPhieu.Number1 == null)
                               {
                                  // throw new Exception("Không được để trống ô nào trong bảng");
                                   checkNull = true;
                                   break;
                               }
                           }
                           if (checkNull)
                           {
                               gridControlA_12.Focus();
                               throw new Exception("Không được để trống ô nào trong bảng");
                           }
                            

                        }
                        else
                        {
                            throw new Exception("Không được để trống ô nào trong bảng");
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                BHXH.Repository.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }
        private bool CheckHaveDataInGridView(DevExpress.XtraGrid.Views.Grid.GridView gridView)
        {
            if (gridView.DataSource is DataView)
            {
                if ((gridView.DataSource as DataView).Count <= 0)
                {
                    return false;
                }
            }
            return true;
        }

        private bool CheckDataNullInGridView(DevExpress.XtraGrid.Views.Grid.GridView gridView, int soCot)
        {
            int count = 0; //Vì ông event changed chua chạy nên ông cuối cùng luôn check là null. Ta bỏ check ông cuối cùng đi
            if (gridView.DataSource is DataView)
            {
                foreach (DataRow row in (gridView.DataSource as DataView).ToTable().Rows)
                {
                    foreach (DataColumn col in (gridView.DataSource as DataView).ToTable().Columns)
                    {
                        for (int i = 1; i <= soCot; i++)
                        {
                            if (col.ColumnName.Contains(string.Format("Number{0}", i)))
                            {
                                //Type t = row[col.ColumnName].GetType();
                                if (!(row[col.ColumnName] is decimal))
                                    if (!(row[col.ColumnName] is long))
                                        if (!(row[col.ColumnName] is int))
                                            count++;
                                string a = row[col.ColumnName].GetType().ToString();
                            }
                        }
                    }
                }
            }

            //Xét xem cột cuối có trống không
            string valueCellAndOfTable = "";


            return true;
        }

        private int CountRowInGriView(DevExpress.XtraGrid.Views.Grid.GridView gridView)
        {
            try
            {
                if (gridView.DataSource is DataView)
                {
                    return (gridView.DataSource as DataView).Count;
                }
            }
            catch (Exception ex)
            { }
            return 0;
        }

        protected override bool CheckFullInfo()
        {
            if (txtMaPhieu.Text.Trim() == "")
                throw new Exception("Chưa số phiếu");
            if (dateNgayPV.EditValue == null)
                throw new Exception("Chưa chọn ngày tháng phỏng vấn");
            if (txtHoTenNguoiDuocPV.Text.Trim() == "")
                throw new Exception("Chưa điền người được PV");
           if(txtGioiTinh.Text.Trim()=="")
               throw new Exception("Thiếu thông tin giới tính");
           if (numericTexBoxNamSinh.Text.Trim() == "")
               throw new Exception("Thiếu thông tin năm sinh");

            if (nmrB4.Text == "") throw new Exception("Thiếu thông tin tại câu 4");
            if (nmrB5.Text == "") throw new Exception("Thiếu thông tin tại câu 5");

            if (nmrB6_1.Text == "") throw new Exception("Thiếu thông tin tại câu 6");
            if (nmrB6_2.Text == "") throw new Exception("Thiếu thông tin tại câu 6");
            if (nmrB7.Text == "") throw new Exception("Thiếu thông tin tại câu 7");
            if (nmrB8_1.Text == "") throw new Exception("Thiếu thông tin tại câu 8");
            if (txtTenNghe.Text.Trim() == "") throw new Exception("Thiếu thông tin tên nghề");
            if (txtDienThoaiLienHe.Text == "") throw new Exception("Thiếu thông tin điện thoại liên hệ");

            if (nmrB10.Text == "") throw new Exception("Thiếu thông tin tại câu 10");
            if (nmrB11.Text == "") throw new Exception("Thiếu thông tin tại câu 11");
            if (!(CheckHaveDataInGridView(gridViewA_12))) throw new Exception("Chưa điền thông tin câu 12");
            if (nmr13.Text == "") throw new Exception("Thiếu thông tin tại câu 13");
            if (nmr14.Text == "" && nmr13.Text.Trim()=="1") throw new Exception("Thiếu thông tin tại câu 14");
            if (nmr14.Text != "" && nmr13.Text.Trim() == "2") throw new Exception("Không được điện thông tin tại câu 14");
            if (nmr15.Text == "") throw new Exception("Thiếu thông tin tại câu 15");

            if (nmrB16_1.Text == "" && nmr15.Text.Trim()=="1") throw new Exception("Thiếu thông tin tại câu 16");
            if (nmrB16_1.Text != "" && nmr15.Text.Trim() == "2") throw new Exception("Không được điện thông tin tại câu 16");

           // if (nmrB16_2.Text != "" && nmr15.Text.Trim() == "1") throw new Exception("Không được điện thông tin tại câu 16(2)");

            if (nmr17.Text == "") throw new Exception("Thiếu thông tin tại câu 17");
           // if (nmr17.Text == "") throw new Exception("Thiếu thông tin tại câu 18");
            if (nmr18.Text == "" && nmr17.Text=="1") throw new Exception("Thiếu thông tin tại câu 18");


            if (nmr19.Text == "") throw new Exception("Thiếu thông tin tại câu 19");
            if (nmr20.Text == "") throw new Exception("Thiếu thông tin tại câu 20");
            if (nmr21.Text == "") throw new Exception("Thiếu thông tin tại câu 21");
            if (nmr22.Text == "") throw new Exception("Thiếu thông tin tại câu 22");
            if (nmr23.Text == "") throw new Exception("Thiếu thông tin tại câu 23");
            if (nmr24.Text == "") throw new Exception("Thiếu thông tin tại câu 24");
            if (nmr25.Text == "") throw new Exception("Thiếu thông tin tại câu 25");
            if (nmr26.Text == "" && nmr25.Text.Trim()=="1") throw new Exception("Thiếu thông tin tại câu 26");
            if (nmr27.Text == "") throw new Exception("Thiếu thông tin tại câu 27");
            if (nmr28.Text == "") throw new Exception("Thiếu thông tin tại câu 28");
            if (nmr29.Text == "") throw new Exception("Thiếu thông tin tại câu 29");
            if (nmr30.Text == "") throw new Exception("Thiếu thông tin tại câu 30");
            if (nmr31.Text == "") throw new Exception("Thiếu thông tin tại câu 31");
            if (nmr32.Text == "") throw new Exception("Thiếu thông tin tại câu 32");
            if (nmr33.Text == "") throw new Exception("Thiếu thông tin tại câu 33");
            if (nmr34.Text == "") throw new Exception("Thiếu thông tin tại câu 34");
            if (nmr35.Text == "") throw new Exception("Thiếu thông tin tại câu 35");
            if (nmr36.Text == "") throw new Exception("Thiếu thông tin tại câu 36");
            if (nmr37.Text == "") throw new Exception("Thiếu thông tin tại câu 37");
            if (nmr38.Text == "") throw new Exception("Thiếu thông tin tại câu 38");
            if (nmr39.Text == "") throw new Exception("Thiếu thông tin tại câu 39");
            if (nmr40.Text == "") throw new Exception("Thiếu thông tin tại câu 40");
            if (txtKienNghiA41_1_a.Text == "") throw new Exception("Thiếu thông tin tại câu 41.1.a Chế độ ốm đau");
            if (txtKienNghiA40_1_b.Text == "") throw new Exception("Thiếu thông tin tại câu 41.1.b Chế độ thai sản");
             if (txtKienNghiA40_1_c.Text == "") throw new Exception("Thiếu thông tin tại câu 41.1.c Chế độ TNLĐ-BNN");
             if (txtKienNghiA40_1_d.Text == "") throw new Exception("Thiếu thông tin tại câu 41.1.e Chế độ hưu trí");
             if (txtKienNghiA40_1_e.Text == "") throw new Exception("Thiếu thông tin tại câu 41.1.e Chế độ đột tuất");


             if (txtKienNghiA28_2.Text == "") throw new Exception("Thiếu thông tin tại câu 41.2 Thủ tục hồ sơ");
             if (txtKienNghiA28_3.Text == "") throw new Exception("Thiếu thông tin tại câu 41.3 Quy định khác");
            //if (nmr42.Text == "") throw new Exception("Thiếu thông tin tại câu 13");
            //if (nmr13.Text == "") throw new Exception("Thiếu thông tin tại câu 13");

            

            return true;
        }

        #region lost focus
        void nmrB10_LostFocus(object sender, EventArgs e)
        {
            if (nmrB10.Text.Trim() == "2") gridControlA_12.Focus();
        }
        void nmr13_LostFocus(object sender, EventArgs e)
        {
            if (nmr13.Text.Trim() == "2") nmr15.Focus();
        }
        void nmr15_LostFocus(object sender, EventArgs e)
        {
            if (nmr15.Text.Trim() == "2") nmr17.Focus();
        }
        void nmrB16_1_LostFocus(object sender, EventArgs e)
        {
            if (nmrB16_1.Text.Trim() == "2") nmr17.Focus();
            if (nmrB16_1.Text.Trim() == "1") nmrB16_2.Focus();
        }
        void nmr17_LostFocus(object sender, EventArgs e)
        {
            if (nmr17.Text.Trim() == "2") nmr19.Focus();
        }
        void nmr20_LostFocus(object sender, EventArgs e)
        {
            if (nmr20.Text.Trim() == "4") txtKhac20.Focus();
            if (nmr20.Text.Trim() != "4" && nmr20.Text.Trim() != "") nmr21.Focus();
        }

        void nmr21_LostFocus(object sender, EventArgs e)
        {

            if (nmr21.Text.Trim() != "3" && nmr21.Text.Trim() != "") nmr22.Focus();
        }

        void nmr22_LostFocus(object sender, EventArgs e)
        {
            //if (nmr22.Text.Trim() == "3") txtKhac21.Focus();
            if (nmr22.Text.Trim() != "1" && nmr21.Text.Trim() != "") nmr23.Focus();
        }
        void nmr24_LostFocus(object sender, EventArgs e)
        {
            // if (nmr24.Text.Trim() == "4") txtKhac20.Focus();
            if (nmr24.Text.Trim() != "3" && nmr24.Text.Trim() != "") nmr25.Focus();
        }
        void nmr25_LostFocus(object sender, EventArgs e)
        {
            //if (nmr25.Text.Trim() != "3" && nmr25.Text.Trim() != "") nmr26.Focus();
            if (nmr25.Text.Trim() == "2") nmr27.Focus();
        }

        void nmr26_LostFocus(object sender, EventArgs e)
        {
            if (nmr26.Text.Trim() != "4" && nmr26.Text.Trim() != "") nmr27.Focus();
            //if (nmr25.Text.Trim() == "2") nmr27.Focus();
        }

        void nmr33_LostFocus(object sender, EventArgs e)
        {
            if (nmr33.Text.Trim() != "3" && nmr33.Text.Trim() != "") nmr34.Focus();
            //if (nmr25.Text.Trim() == "2") nmr27.Focus();
        }

        void nmr34_LostFocus(object sender, EventArgs e)
        {
            if (nmr34.Text.Trim() != "3" && nmr34.Text.Trim() != "") nmr35.Focus();
            //if (nmr25.Text.Trim() == "2") nmr27.Focus();
        }
        void nmr40_LostFocus(object sender, EventArgs e)
        {
            if (nmr40.Text.Trim() != "3" && nmr40.Text.Trim() != "") txtKienNghiA41_1_a.Focus();
            //if (nmr25.Text.Trim() == "2") nmr27.Focus();
        }

        #endregion lost focus

        private void FrmEditPhieuB_Load(object sender, EventArgs e)
        {
            try
            {
                lblTitle.Text = "BỘ LAO ĐỘNG -THƯƠNG BINH VÀ XÃ HỘI\nVỤ BẢO HIỂM XÃ HỘI";
                lblLoaiPhieu.Text = "Phiếu B";
                lblDienGiai.Text = "PHIẾU ĐIỀU TRA TÌNH HÌNH THỰC HIỆN LUẬT BẢO HIỂM \n " +
                    "XÃ HỘI VỀ BẢO HIỂM XÃ HỘI BẮT BUỘC\n " +
                    "(Dành cho người lao động)";

                nmrB10.LostFocus += new EventHandler(nmrB10_LostFocus);
                nmrB16_1.LostFocus += new EventHandler(nmrB16_1_LostFocus);
                nmr13.LostFocus += new EventHandler(nmr13_LostFocus);
                nmr15.LostFocus += new EventHandler(nmr15_LostFocus);
                nmr17.LostFocus += new EventHandler(nmr17_LostFocus);
                nmr20.LostFocus += new EventHandler(nmr20_LostFocus);
                nmr21.LostFocus += new EventHandler(nmr21_LostFocus);
                nmr22.LostFocus += new EventHandler(nmr22_LostFocus);
                nmr24.LostFocus += new EventHandler(nmr24_LostFocus);
                nmr25.LostFocus += new EventHandler(nmr25_LostFocus);
                nmr26.LostFocus += new EventHandler(nmr26_LostFocus);
                nmr33.LostFocus += new EventHandler(nmr33_LostFocus);
                nmr34.LostFocus += new EventHandler(nmr34_LostFocus);
                nmr40.LostFocus += new EventHandler(nmr40_LostFocus);


                //dsPhieu_DangBang.PhieuB_Cau14.ColumnChanged += new DataColumnChangeEventHandler(PhieuB_Cau14_ColumnChanged);
                gridViewA_12.KeyDown += new KeyEventHandler(gridViewA_12_KeyDown);

                //LoadCauTraLoiDangBangPhieuB();
                // LoadCauTraLoiDangBangPhieuA();
                LoadCauTraLoiDangBangPhieuB();

                //gridControlB_14.DataSource = dsPhieu_DangBang.PhieuB_Cau14;

                #region Load thông tin phiếu
                if (_PhieuA == null)
                {
                    //_PhieuB = DanhMucPhieu.Instance.NewDoiTuong();
                    SetEditState(EditState.CreatingNew);
                    //Lay gia tri tang dan
                    txtMaPhieu.Text = "B";
                    txtMaPhieu.Text = _repPhieu.getNextMaPhieu("PhieuB");

                    //phieuObj = new BHXH.LinqRepository.Phieu();
                    _PhieuA = new BHXH.LinqRepository.Phieu();
                }
                else
                {

                    NhanVienDT_GSRepository nhanviendieutraRepository = new NhanVienDT_GSRepository();
                    if (_PhieuA.NVDieuTraID.HasValue)
                    {
                        NhanVienDT_G nhanvienDTG = nhanviendieutraRepository.GetByID(_PhieuA.NVDieuTraID.Value);
                        if (nhanvienDTG != null)
                        {
                            txtDieuTraVien.Text = nhanvienDTG.HoTen;
                        }
                    }

                    
                    if (_PhieuA.NVGiamSatID.HasValue)
                    {
                        NhanVienDT_G nhanvienDTG = nhanviendieutraRepository.GetByID(_PhieuA.NVGiamSatID.Value);
                        if (nhanvienDTG != null)
                        {
                            txtGiamSatVien.Text = nhanvienDTG.HoTen;
                        }
                    }

                    txtMaPhieu.Text = _PhieuA.MaPhieu;
                   CoQuan coQuan = _repCoQuan.GetByID(_PhieuA.CoQuanID);
                    dateNgayPV.DateTime = _PhieuA.NgayPV;

                    Lists.NguoiDuocPV nguoiduocpv = Lists.DanhMucNguoiDuocPV.Instance.GetByID(_PhieuA.NguoiTraLoiPVID);
                    if (nguoiduocpv != null)
                    {
                        txtHoTenNguoiDuocPV.Text = nguoiduocpv.HoTen;
                        if (nguoiduocpv.GioiTinh) txtGioiTinh.Text = "1";
                        else txtGioiTinh.Text = "2";
                        numericTexBoxNamSinh.Text = nguoiduocpv.NamSinh.ToString();
                        numericTexBoxTuoiTron.Text = nguoiduocpv.Tuoi.ToString();
                        txtDienThoaiLienHe.Text = nguoiduocpv.DienThoai;
                    }
                    if (coQuan != null)
                        txtTenCoQuan.Text = coQuan.Ten;

                   // _NguoiTraLoiPV2 = _PhieuA.NguoiTraLoiPV;
                   // txtHoTenNguoiDuocPV.Text = _NguoiTraLoiPV2.HoTen;
                   

                    BHXH.LinqRepository.ChiTietPhieu ctp = null;

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 4);
                    if (ctp != null)
                    {
                        nmrB4.Text = ctp.CauTraLoiFIX1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 5);
                    if (ctp != null)
                    {
                        nmrB5.Text = ctp.CauTraLoiFIX1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 6);
                    if (ctp != null)
                    {
                        nmrB6_1.Text = ctp.CauTraLoiFIX1;
                        nmrB6_2.Text = ctp.CauTraLoiFIX2;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 7);
                    if (ctp != null)
                    {
                        nmrB7.Text = ctp.CauTraLoiFIX1;
                    }


                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 8);
                    if (ctp != null)
                    {
                        nmrB8_1.Text = ctp.CauTraLoiFIX1;
                        txtTenNghe.Text = ctp.CauTraLoiText1;
                        txtMaNghe.Text = ctp.CauTraLoiText2;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 10);
                    if (ctp != null)
                    {
                        nmrB10.Text = ctp.CauTraLoiFIX1;
                    }
                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 11);
                    if (ctp != null)
                    {
                        nmrB11.Text = ctp.CauTraLoiFIX1;
                    }


                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 13);
                    if (ctp != null)
                    {
                        nmr13.Text = ctp.CauTraLoiFIX1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 14);
                    if (ctp != null)
                    {
                        nmr14.Text = ctp.CauTraLoiFIX1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 15);
                    if (ctp != null)
                    {
                        nmr15.Text = ctp.CauTraLoiFIX1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 16);
                    if (ctp != null)
                    {
                        nmrB16_1.Text = ctp.CauTraLoiFIX1;
                        nmrB16_2.Text = ctp.CauTraLoiFIX2;
                        txtKhacB16.Text = ctp.CauTraLoiText1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 17);
                    if (ctp != null)
                    {
                        nmr17.Text = ctp.CauTraLoiFIX1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 18);
                    if (ctp != null)
                    {
                        nmr18.Text = ctp.CauTraLoiFIX1;
                        txtKhac18.Text = ctp.CauTraLoiText1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 19);
                    if (ctp != null)
                    {
                        nmr19.Text = ctp.CauTraLoiFIX1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 20);
                    if (ctp != null)
                    {
                        nmr20.Text = ctp.CauTraLoiFIX1;
                        txtKhac20.Text = ctp.CauTraLoiText1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 21);
                    if (ctp != null)
                    {
                        nmr21.Text = ctp.CauTraLoiFIX1;
                        txtKhac21.Text = ctp.CauTraLoiText1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 22);
                    if (ctp != null)
                    {
                        nmr22.Text = ctp.CauTraLoiFIX1;
                        txtKhac22.Text = ctp.CauTraLoiText1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 23);
                    if (ctp != null)
                    {
                        nmr23.Text = ctp.CauTraLoiFIX1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 24);
                    if (ctp != null)
                    {
                        nmr24.Text = ctp.CauTraLoiFIX1;
                        txtKhac24.Text = ctp.CauTraLoiText1;
                    }


                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 25);
                    if (ctp != null)
                    {
                        nmr25.Text = ctp.CauTraLoiFIX1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 26);
                    if (ctp != null)
                    {
                        nmr26.Text = ctp.CauTraLoiFIX1;
                        txtKhac26.Text = ctp.CauTraLoiText1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 27);
                    if (ctp != null)
                    {
                        nmr27.Text = ctp.CauTraLoiFIX1;
                    }


                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 28);
                    if (ctp != null)
                    {
                        nmr28.Text = ctp.CauTraLoiFIX1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 29);
                    if (ctp != null)
                    {
                        nmr29.Text = ctp.CauTraLoiFIX1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 30);
                    if (ctp != null)
                    {
                        nmr30.Text = ctp.CauTraLoiFIX1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 31);
                    if (ctp != null)
                    {
                        nmr31.Text = ctp.CauTraLoiFIX1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 32);
                    if (ctp != null)
                    {
                        nmr32.Text = ctp.CauTraLoiFIX1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 33);
                    if (ctp != null)
                    {
                        nmr33.Text = ctp.CauTraLoiFIX1;
                        txtKhac33.Text = ctp.CauTraLoiFIX1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 34);
                    if (ctp != null)
                    {
                        nmr34.Text = ctp.CauTraLoiFIX1;
                        txtKhac34.Text = ctp.CauTraLoiFIX1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 35);
                    if (ctp != null)
                    {
                        nmr35.Text = ctp.CauTraLoiFIX1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 36);
                    if (ctp != null)
                    {
                        nmr36.Text = ctp.CauTraLoiFIX1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 37);
                    if (ctp != null)
                    {
                        nmr37.Text = ctp.CauTraLoiFIX1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 38);
                    if (ctp != null)
                    {
                        nmr38.Text = ctp.CauTraLoiFIX1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 39);
                    if (ctp != null)
                    {
                        nmr39.Text = ctp.CauTraLoiFIX1;
                    }

                    ctp = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 40);
                    if (ctp != null)
                    {
                        nmr40.Text = ctp.CauTraLoiFIX1;
                        txtKhac40.Text = ctp.CauTraLoiFIX1;
                    }

                    Lists.KienNghi kienNghi = null;

                    kienNghi = Lists.DanhMucKienNghi.GetByPhieuID_MaCau_MaCap1_MaCap2(_PhieuA.ID, 41, "1", "a");
                    if (kienNghi != null)
                    {
                        txtKienNghiA41_1_a.Text = kienNghi.Text;
                    }

                    kienNghi = Lists.DanhMucKienNghi.GetByPhieuID_MaCau_MaCap1_MaCap2(_PhieuA.ID, 41, "1", "b");
                    if (kienNghi != null)
                    {
                        txtKienNghiA40_1_b.Text = kienNghi.Text;
                    }

                    kienNghi = Lists.DanhMucKienNghi.GetByPhieuID_MaCau_MaCap1_MaCap2(_PhieuA.ID, 41, "1", "c");
                    if (kienNghi != null)
                    {
                        txtKienNghiA40_1_c.Text = kienNghi.Text;
                    }

                    kienNghi = Lists.DanhMucKienNghi.GetByPhieuID_MaCau_MaCap1_MaCap2(_PhieuA.ID, 41, "1", "d");
                    if (kienNghi != null)
                    {
                        txtKienNghiA40_1_d.Text = kienNghi.Text;
                    }

                    kienNghi = Lists.DanhMucKienNghi.GetByPhieuID_MaCau_MaCap1_MaCap2(_PhieuA.ID, 41, "1", "e");
                    if (kienNghi != null)
                    {
                        txtKienNghiA40_1_e.Text = kienNghi.Text;
                    }



                    kienNghi = Lists.DanhMucKienNghi.GetByPhieuID_MaCau_MaCap1_MaCap2(_PhieuA.ID, 41, "2", "");
                    if (kienNghi != null)
                    {
                        txtKienNghiA28_2.Text = kienNghi.Text;
                    }

                    kienNghi = Lists.DanhMucKienNghi.GetByPhieuID_MaCau_MaCap1_MaCap2(_PhieuA.ID, 41, "3", "");
                    if (kienNghi != null)
                    {
                        txtKienNghiA28_3.Text = kienNghi.Text;
                    }


                    SetEditState(EditState.Saved);
                }

                #endregion

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                txtMaPhieu.SelectAll();
                txtMaPhieu.Focus();

                //#region Format Grid
                //if (_EditState != EditState.CreatingNew)
                //{
                //    gridViewB_14.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None;
                //}
                //#endregion
            }
        }


        private void txtTenCoQuan_TextChanged(object sender, EventArgs e)
        {

        }

        private void txtKienNghiA40_1_c_TextChanged(object sender, EventArgs e)
        {

        }

        #region set cau tra loi
        protected void SetCauTraLoi(CustomControls.NumericTexBox nmrX, string loaiPhieu, string maTraLoi, int maCau, int levelTraLoi, System.Windows.Forms.Label lblShow)
        {
            Lists.CauHoi_DA cauHoi_DA = Lists.DanhMucCauHoi_DA.GetByLoaiPhieu_MaCau(loaiPhieu, maCau, levelTraLoi);
            lblShow.Text = "";
            if (maTraLoi == "") return;
            if (cauHoi_DA != null)
            {
                string[] traloi1 = Regex.Split(cauHoi_DA.TraLoi1, "::");
                if (maTraLoi == traloi1[0])
                    lblShow.Text = traloi1[1];
                string[] traloi2 = Regex.Split(cauHoi_DA.TraLoi2, "::");
                if (maTraLoi == traloi2[0])
                    lblShow.Text = traloi2[1];
                string[] traloi3 = Regex.Split(cauHoi_DA.TraLoi3, "::");
                if (maTraLoi == traloi3[0])
                    lblShow.Text = traloi3[1];
                string[] traloi4 = Regex.Split(cauHoi_DA.TraLoi4, "::");
                if (maTraLoi == traloi4[0])
                    lblShow.Text = traloi4[1];
                string[] traloi5 = Regex.Split(cauHoi_DA.TraLoi5, "::");
                if (maTraLoi == traloi5[0])
                    lblShow.Text = traloi5[1];
                string[] traloi6 = Regex.Split(cauHoi_DA.TraLoi6, "::");
                if (maTraLoi == traloi6[0])
                    lblShow.Text = traloi6[1];
                string[] traloi7 = Regex.Split(cauHoi_DA.TraLoi7, "::");
                if (maTraLoi == traloi7[0])
                    lblShow.Text = traloi7[1];
                string[] traloi8 = Regex.Split(cauHoi_DA.TraLoi8, "::");
                if (maTraLoi == traloi8[0])
                    lblShow.Text = traloi8[1];
                string[] traloi9 = Regex.Split(cauHoi_DA.TraLoi9, "::");
                if (maTraLoi == traloi9[0])
                    lblShow.Text = traloi9[1];
                string[] traloi10 = Regex.Split(cauHoi_DA.TraLoi10, "::");
                if (maTraLoi == traloi10[0])
                    lblShow.Text = traloi10[1];
            }

            if (lblShow.Text == "" && nmrX.Text != "0")
            {
                MessageBox.Show("Không có trong danh mục câu trả lời.\nBạn cần nhập lại");
                nmrX.Text = "";
                nmrX.Focus();
            }
        }


        protected void SetCauTraLoiDacBiet(CustomControls.NumericTexBox nmrX, string maTraLoi,
                                            int maCau, int levelTraLoi, System.Windows.Forms.Label lblShow)
        {
            Lists.CauHoi_DA cauHoi_DA = Lists.DanhMucCauHoi_DA.GetByLoaiPhieu_MaCau("PhieuB", maCau, levelTraLoi);
            lblShow.Text = "";
            if (maTraLoi == "") return;
            if (cauHoi_DA != null)
            {
                string[] traloi1 = Regex.Split(cauHoi_DA.TraLoi1, "::");
                if (maTraLoi.Contains(traloi1[0].Trim()))
                    if (traloi1.Length >= 2) lblShow.Text = lblShow.Text + ", " + traloi1[1].Trim();
                string[] traloi2 = Regex.Split(cauHoi_DA.TraLoi2, "::");
                if (maTraLoi.Contains(traloi2[0].Trim()))
                    if (traloi2.Length >= 2) lblShow.Text = lblShow.Text + ", " + traloi2[1].Trim();
                string[] traloi3 = Regex.Split(cauHoi_DA.TraLoi3, "::");
                if (maTraLoi.Contains(traloi3[0].Trim()))
                    if (traloi3.Length >= 2) lblShow.Text = lblShow.Text + ", " + traloi3[1].Trim();
                string[] traloi4 = Regex.Split(cauHoi_DA.TraLoi4, "::");
                if (maTraLoi.Contains(traloi4[0].Trim()))
                    if (traloi4.Length >= 2) lblShow.Text = lblShow.Text + ", " + traloi4[1].Trim();
                string[] traloi5 = Regex.Split(cauHoi_DA.TraLoi5, "::");
                if (maTraLoi.Contains(traloi5[0].Trim()))
                    if (traloi5.Length >= 2) lblShow.Text = lblShow.Text + ", " + traloi5[1].Trim();
                string[] traloi6 = Regex.Split(cauHoi_DA.TraLoi6, "::");
                if (maTraLoi.Contains(traloi6[0].Trim()))
                    if (traloi6.Length >= 2) lblShow.Text = lblShow.Text + ", " + traloi6[1].Trim();
                string[] traloi7 = Regex.Split(cauHoi_DA.TraLoi7, "::");
                if (maTraLoi.Contains(traloi7[0].Trim()))
                    if (traloi7.Length >= 2) lblShow.Text = lblShow.Text + ", " + traloi7[1].Trim();
                string[] traloi8 = Regex.Split(cauHoi_DA.TraLoi8, "::");
                if (maTraLoi.Contains(traloi8[0].Trim()))
                    if (traloi8.Length >= 2) lblShow.Text = lblShow.Text + ", " + traloi8[1].Trim();
                string[] traloi9 = Regex.Split(cauHoi_DA.TraLoi9, "::");
                if (maTraLoi.Contains(traloi9[0].Trim()))
                    if (traloi9.Length >= 2) lblShow.Text = lblShow.Text + ", " + traloi9[1].Trim();
                string[] traloi10 = Regex.Split(cauHoi_DA.TraLoi10, "::");
                if (maTraLoi.Contains(traloi10[0].Trim()))
                    if (traloi10.Length >= 2) lblShow.Text = lblShow.Text + ", " + traloi10[1].Trim();
            }

            if (lblShow.Text == "" && nmrX.Text != "0")
            {
                MessageBox.Show("Không có trong danh mục câu trả lời.\nBạn cần nhập lại");
                nmrX.Text = "";
                nmrX.Focus();
            }
        }

        #endregion set cau tra loi


        #region Text change - key down - key press


        private void nmrB10_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmrB10.Text.Trim();
            SetCauTraLoi(nmrB10, "PhieuB", maTraLoi, 10, 0, tlB10);
        }
        #region Key down - key press
        private void txtMaPhieu_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return || e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab || e.KeyCode == Keys.Down || e.KeyCode == Keys.Right)
            {
                txtTenCoQuan.Focus();
            }
        }

        private void txtTenCoQuan_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab || e.KeyCode == Keys.Down || e.KeyCode == Keys.Right)
            {
                cmbTinhThanh.Focus();
            }

            if (e.KeyCode == Keys.Up)
            {
                txtMaPhieu.Focus();
            }
        }

        private void cmbTinhThanh_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab || e.KeyCode == Keys.Down || e.KeyCode == Keys.Right)
            {
                cmbQuanHuyen.Focus();
            }

            if (e.KeyCode == Keys.Up)
            {
                txtTenCoQuan.Focus();
            }
        }

        private void cmbQuanHuyen_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Tab || e.KeyCode == Keys.Down || e.KeyCode == Keys.Right)
            {
                dateNgayPV.Focus();
            }

            if (e.KeyCode == Keys.Up)
            {
                cmbQuanHuyen.Focus();
            }
        }

        private void txtMaPhieu_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == Convert.ToChar(Keys.Enter))
            {
                txtTenCoQuan.Focus();
            }
        }
        #endregion Key down - key press
        private void nmrB11_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmrB11.Text.Trim();
            SetCauTraLoi(nmrB11, "PhieuB", maTraLoi, 11, 0, tlB11);
        }
        private void nmr13_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmr13.Text.Trim();
            SetCauTraLoi(nmr13, "PhieuB", maTraLoi, 13, 0, tl13);
        }
        private void nmr14_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmr14.Text.Trim();
            SetCauTraLoi(nmr14, "PhieuB", maTraLoi, 14, 0, tl14);
        }
        private void nmr15_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmr15.Text.Trim();
            SetCauTraLoi(nmr15, "PhieuB", maTraLoi, 15, 0, tl15);
        }
        private void nmrB16_1_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmrB16_1.Text.Trim();
            SetCauTraLoi(nmrB16_1, "PhieuB", maTraLoi, 16, 0, tlB16_1);
        }
        private void nmrB16_2_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmrB16_2.Text.Trim();
            SetCauTraLoiDacBiet(nmrB16_2, maTraLoi, 16, 1, tlB16_2);
        }
        private void nmr17_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmr17.Text.Trim();
            SetCauTraLoi(nmr17, "PhieuB", maTraLoi, 17, 0, tl17);
        }
        private void nmr18_TextChanged(object sender, EventArgs e)
        {
            //string maTraLoi = nmr18.Text.Trim();
            //SetCauTraLoi(nmr18, "PhieuB", maTraLoi, 18, 1, tl18);
            string maTraLoi = nmr18.Text.Trim();
            SetCauTraLoiDacBiet(nmr18, maTraLoi, 18, 0, tl18);
        }
        private void nmr19_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmr19.Text.Trim();
            SetCauTraLoi(nmr19, "PhieuB", maTraLoi, 19, 0, tl19);
        }
        private void nmr20_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmr20.Text.Trim();
            SetCauTraLoi(nmr20, "PhieuB", maTraLoi, 20, 0, tl20);
        }

        private void nmr21_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmr21.Text.Trim();
            SetCauTraLoi(nmr21, "PhieuB", maTraLoi, 21, 0, tl21);
        }

        private void nmr22_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmr22.Text.Trim();
            SetCauTraLoi(nmr22, "PhieuB", maTraLoi, 22, 0, tl22);
        }

        private void nmr23_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmr23.Text.Trim();
            SetCauTraLoi(nmr23, "PhieuB", maTraLoi, 23, 0, tl23);
        }
        private void nmr24_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmr24.Text.Trim();
            SetCauTraLoi(nmr24, "PhieuB", maTraLoi, 24, 0, tl24);
        }
        private void nmr25_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmr25.Text.Trim();
            SetCauTraLoi(nmr25, "PhieuB", maTraLoi, 25, 0, tl25);
        }

        private void nmr26_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmr26.Text.Trim();
            SetCauTraLoi(nmr26, "PhieuB", maTraLoi, 26, 0, tl26);
        }

        private void nmr27_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmr27.Text.Trim();
            SetCauTraLoi(nmr27, "PhieuB", maTraLoi, 27, 0, tl27);
        }

        private void nmr28_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmr28.Text.Trim();
            SetCauTraLoi(nmr28, "PhieuB", maTraLoi, 28, 0, tl28);
        }

        private void nmr29_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmr29.Text.Trim();
            SetCauTraLoi(nmr29, "PhieuB", maTraLoi, 29, 0, tl29);
        }

        private void nmr30_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmr30.Text.Trim();
            SetCauTraLoi(nmr30, "PhieuB", maTraLoi, 30, 0, tl30);
        }

        private void nmr31_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmr31.Text.Trim();
            SetCauTraLoi(nmr31, "PhieuB", maTraLoi, 31, 0, tl31);
        }

        private void nmr32_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmr32.Text.Trim();
            SetCauTraLoi(nmr32, "PhieuB", maTraLoi, 32, 0, tl32);
        }

        private void nmr33_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmr33.Text.Trim();
            SetCauTraLoi(nmr33, "PhieuB", maTraLoi, 33, 0, tl33);
        }

        private void nmr34_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmr34.Text.Trim();
            SetCauTraLoi(nmr34, "PhieuB", maTraLoi, 34, 0, tl34);
        }

        private void nmr35_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmr35.Text.Trim();
            SetCauTraLoi(nmr35, "PhieuB", maTraLoi, 35, 0, tl35);
        }

        private void nmr36_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmr36.Text.Trim();
            SetCauTraLoi(nmr36, "PhieuB", maTraLoi, 36, 0, tl36);
        }

        private void nmr37_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmr37.Text.Trim();
            SetCauTraLoi(nmr37, "PhieuB", maTraLoi, 37, 0, tl37);
        }

        private void nmr38_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmr38.Text.Trim();
            SetCauTraLoi(nmr38, "PhieuB", maTraLoi, 38, 0, tl38);
        }

        private void nmr39_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmr39.Text.Trim();
            SetCauTraLoi(nmr39, "PhieuB", maTraLoi, 39, 0, tl39);
        }

        private void nmr40_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmr40.Text.Trim();
            SetCauTraLoi(nmr40, "PhieuB", maTraLoi, 40, 0, tl40);
        }
        #endregion Text change - key down - key press


        /// <summary>
        /// Save data
        /// </summary>
        protected override void Save()
        {
            try
            {
                CheckFullInfo();
                try
                {
                    BHXH.LinqRepository.Phieu phieuA = new BHXH.LinqRepository.Phieu();
                    if (_EditState == BHXH.Repository.EditState.CreatingNew)    //mỗi phiếu tạo mới là 1 người được PV mới
                    {
                        NhanVienDT_G nhanvienDTGS = new NhanVienDT_G();
                        if (txtDieuTraVien.Text.Trim() != "")
                        {
                            nhanvienDTGS.HoTen = txtDieuTraVien.Text;

                            NhanVienDT_GSRepository dieutravienRepository = new NhanVienDT_GSRepository();
                            dieutravienRepository.InsertOne(nhanvienDTGS);
                            dieutravienRepository.SubmitChanges();
                            phieuA.NVDieuTraID = nhanvienDTGS.ID;
                        }

                        if (txtGiamSatVien.Text.Trim() != "")
                        {
                            nhanvienDTGS = new NhanVienDT_G();
                            nhanvienDTGS.HoTen = txtDieuTraVien.Text;

                            NhanVienDT_GSRepository dieutravienRepository = new NhanVienDT_GSRepository();
                            dieutravienRepository.InsertOne(nhanvienDTGS);
                            dieutravienRepository.SubmitChanges();
                            phieuA.NVGiamSatID = nhanvienDTGS.ID;
                        }
                        NguoiTraLoiPV nguoitraloi = new NguoiTraLoiPV();
                        nguoitraloi.HoTen = txtHoTenNguoiDuocPV.Text.Trim();
                        if (txtGioiTinh.Text == "1") nguoitraloi.GioiTinh = true;
                        else nguoitraloi.GioiTinh = false;

                        nguoitraloi.NamSinh = Convert.ToInt32(numericTexBoxNamSinh.Text.Trim());
                        nguoitraloi.Tuoi = Convert.ToInt32(numericTexBoxTuoiTron.Text.Trim());
                        //nguoitraloi.ChucVu = txtChucVu.Text.Trim();
                        nguoitraloi.DienThoai = txtDienThoaiLienHe.Text.Trim();

                        //nguoitraloi.ID = Guid.NewGuid();

                        _repNguoiTraloiPV.InsertOne(nguoitraloi);
                        _repNguoiTraloiPV.SubmitChanges();
                        //
                        BHXH.LinqRepository.CoQuan coQuan = new BHXH.LinqRepository.CoQuan();
                        coQuan.Ten = txtTenCoQuan.Text.Trim();
                        //coQuan.TinhID = (Guid)cmbTinhThanh.EditValue;
                        //coQuan.QuanHuyenID = (Guid)cmbQuanHuyen.EditValue;
                        //coQuan.PhuongXa = txtPhuongXa.Text.Trim();
                        //coQuan.Sonha_Pho = txtSoNha.Text.Trim();

                        _repCoQuan.InsertOne(coQuan);
                        _repCoQuan.SubmitChanges();
                        //
                        #region Phieu
                        phieuA.MaPhieu = "B" + txtMaPhieu.Text.Trim().Replace("B", "");
                        phieuA.LoaiPhieu = "PhieuB";
                        //phieuA.CoQuan = coQuan;
                        phieuA.NgayPV = dateNgayPV.DateTime;
                        // phieuA.NguoiTraLoiPV = nguoitraloi;

                        phieuA.CoQuanID = coQuan.ID;
                        phieuA.NguoiTraLoiPVID = nguoitraloi.ID;
                        #endregion

                        _repPhieu.InsertOne(phieuA);
                        _repPhieu.SubmitChanges();

                    }
                    else
                    {

                        var nguoiDuocPV = _repNguoiTraloiPV.GetById(_PhieuA.NguoiTraLoiPVID);
                        nguoiDuocPV.HoTen = txtHoTenNguoiDuocPV.Text.Trim();
                        if (txtGioiTinh.Text == "1") nguoiDuocPV.GioiTinh = true;
                        else nguoiDuocPV.GioiTinh = false;
                        nguoiDuocPV.NamSinh = Convert.ToInt32(numericTexBoxNamSinh.Text.Trim());
                        nguoiDuocPV.Tuoi = Convert.ToInt32(numericTexBoxTuoiTron.Text.Trim());
                        // nguoiDuocPV.ChucVu = txtChucVu.Text.Trim();
                        nguoiDuocPV.DienThoai = txtDienThoaiLienHe.Text.Trim();

                        nguoipv = nguoiDuocPV;
                        _repNguoiTraloiPV.EditOne(nguoiDuocPV);
                        //
                        // _repCoQuan.GetById
                        var coQuan = _repCoQuan.GetByID(_PhieuA.CoQuanID);
                        coQuan.Ten = txtTenCoQuan.Text.Trim();
                        //coQuan.TinhID = (Guid)cmbTinhThanh.EditValue;
                        //coQuan.QuanHuyenID = (Guid)cmbQuanHuyen.EditValue;
                        //coQuan.PhuongXa = txtPhuongXa.Text.Trim();
                        //coQuan.Sonha_Pho = txtSoNha.Text.Trim();
                        _repCoQuan.EditOne(coQuan);
                        _repCoQuan.SubmitChanges();
                        //
                        phieuA = _repPhieu.GetOneById(_PhieuA.ID);

                        #region Phieu
                        phieuA.MaPhieu = "B" + txtMaPhieu.Text.Trim().Replace("B", "");
                        phieuA.LoaiPhieu = "PhieuB";
                        // phieuA.CoQuan = coQuan;
                        phieuA.NgayPV = dateNgayPV.DateTime;
                        phieuA.NguoiTraLoiPVID = nguoiDuocPV.ID;
                        _repPhieu.SubmitChanges();
                        #endregion

                    }
                    //  _repPhieu.SubmitChanges();

                    #region Phieu Dang Bang

                    InsertOrUpdateToDB(phieuA, source12, 12);

                    #endregion

                    #region chi tiết phiếu


                    var chiTietPhieuA4 = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 4);
                    if (chiTietPhieuA4 != null)
                    {
                        chiTietPhieuA4.CauTraLoiFIX1 = nmrB4.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieuA4);
                    }
                    else
                    {
                        BHXH.LinqRepository.ChiTietPhieu chiTietphieuANew4 = new BHXH.LinqRepository.ChiTietPhieu();
                        // chiTietphieuANew4.Phieu = phieuA;
                        chiTietphieuANew4.PhieuID = phieuA.ID;
                        chiTietphieuANew4.MaCau = 4;
                        chiTietphieuANew4.CauTraLoiFIX1 = nmrB4.Text;
                        _repChiTietPhieu.InsertOne(chiTietphieuANew4);
                    }


                    var chiTietPhieuA5 = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 5);
                    if (chiTietPhieuA5 != null)
                    {
                        chiTietPhieuA5.CauTraLoiFIX1 = nmrB5.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieuA5);
                    }
                    else
                    {
                        BHXH.LinqRepository.ChiTietPhieu chiTietphieuANew5 = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietphieuANew5.PhieuID = phieuA.ID;
                        chiTietphieuANew5.MaCau = 5;
                        chiTietphieuANew5.CauTraLoiFIX1 = nmrB5.Text;
                        _repChiTietPhieu.InsertOne(chiTietphieuANew5);
                    }


                    var chiTietPhieuA6_1 = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 6);
                    if (chiTietPhieuA6_1 != null)
                    {
                        chiTietPhieuA6_1.CauTraLoiFIX1 = nmrB6_1.Text;
                        chiTietPhieuA6_1.CauTraLoiFIX2 = nmrB6_2.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieuA6_1);
                    }
                    else
                    {
                        BHXH.LinqRepository.ChiTietPhieu chiTietphieuANew6 = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietphieuANew6.PhieuID = phieuA.ID;
                        chiTietphieuANew6.MaCau = 6;
                        chiTietphieuANew6.CauTraLoiFIX1 = nmrB6_1.Text;
                        chiTietphieuANew6.CauTraLoiFIX2 = nmrB6_2.Text;
                        _repChiTietPhieu.InsertOne(chiTietphieuANew6);
                    }
                    _repChiTietPhieu.SubmitChanges();

                    var chiTietPhieuA7 = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 7);
                    if (chiTietPhieuA7 != null)
                    {
                        chiTietPhieuA7.CauTraLoiFIX1 = nmrB7.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieuA7);
                    }
                    else
                    {
                        chiTietPhieuA7 = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieuA7.PhieuID = phieuA.ID;
                        chiTietPhieuA7.MaCau = 7;
                        chiTietPhieuA7.CauTraLoiFIX1 = nmrB7.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieuA7);
                    }


                    var chiTietPhieuA8 = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 8);
                    if (chiTietPhieuA8 != null)
                    {
                        // chiTietPhieuA8.CauTraLoiFIX1 = nmr8.Text;
                        chiTietPhieuA8.CauTraLoiText1 = txtTenNghe.Text;
                        chiTietPhieuA8.CauTraLoiText2 = txtMaNghe.Text;
                        chiTietPhieuA8.CauTraLoiFIX1 = nmrB8_1.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieuA8);
                    }
                    else
                    {
                        chiTietPhieuA8 = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieuA8.PhieuID = phieuA.ID;
                        chiTietPhieuA8.MaCau = 8;
                        chiTietPhieuA8.CauTraLoiText1 = txtTenNghe.Text;
                        chiTietPhieuA8.CauTraLoiText2 = txtMaNghe.Text;
                        chiTietPhieuA8.CauTraLoiFIX1 = nmrB8_1.Text;
                        // _repChiTietPhieu.EditOne(chiTietPhieuA8);
                        _repChiTietPhieu.InsertOne(chiTietPhieuA8);

                    }
                    //10
                    ChiTietPhieu chiTietPhieu;
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 10);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmrB10.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 10;
                        chiTietPhieu.CauTraLoiFIX1 = nmrB10.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }

                    //11

                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 11);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmrB11.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 11;
                        chiTietPhieu.CauTraLoiFIX1 = nmrB11.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }
                    //12 là grid

                    //13
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 13);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr13.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 13;
                        chiTietPhieu.CauTraLoiFIX1 = nmr13.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }
                    //14
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 14);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr14.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 14;
                        chiTietPhieu.CauTraLoiFIX1 = nmr14.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }
                    //15
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 15);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr15.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 15;
                        chiTietPhieu.CauTraLoiFIX1 = nmr15.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }

                   
                    //16
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 16);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmrB16_1.Text;
                        chiTietPhieu.CauTraLoiFIX2 = nmrB16_2.Text;
                        chiTietPhieu.CauTraLoiText1 = txtKhacB16.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 16;
                        chiTietPhieu.CauTraLoiFIX1 = nmrB16_1.Text;
                        chiTietPhieu.CauTraLoiFIX2 = nmrB16_2.Text;
                        chiTietPhieu.CauTraLoiText1 = txtKhacB16.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }

                    //17
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 17);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr17.Text;

                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 17;
                        chiTietPhieu.CauTraLoiFIX1 = nmr17.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }
                    //18
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 18);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr18.Text;
                        chiTietPhieu.CauTraLoiText1 = txtKhac18.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 18;
                        chiTietPhieu.CauTraLoiFIX1 = nmr18.Text;
                        chiTietPhieu.CauTraLoiText1 = txtKhac18.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }


                    

                    //19
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 19);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr19.Text;
                        // chiTietPhieu.CauTraLoiText1 = txtKhac18.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 19;
                        chiTietPhieu.CauTraLoiFIX1 = nmr19.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }

                    //20
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 20);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr20.Text;
                        chiTietPhieu.CauTraLoiText1 = txtKhac20.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 20;
                        chiTietPhieu.CauTraLoiFIX1 = nmr20.Text;
                        chiTietPhieu.CauTraLoiText1 = txtKhac20.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }



                    //21
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 21);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr21.Text;
                        chiTietPhieu.CauTraLoiText1 = txtKhac21.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 21;
                        chiTietPhieu.CauTraLoiFIX1 = nmr21.Text;
                        chiTietPhieu.CauTraLoiText1 = txtKhac21.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }


                    //22
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 22);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr22.Text;
                        chiTietPhieu.CauTraLoiText1 = txtKhac22.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 22;
                        chiTietPhieu.CauTraLoiFIX1 = nmr22.Text;
                        chiTietPhieu.CauTraLoiText1 = txtKhac22.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }

                    //23
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 23);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr23.Text;
                        //chiTietPhieu.CauTraLoiText1 = txtKhac23.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 23;
                        chiTietPhieu.CauTraLoiFIX1 = nmr23.Text;
                        //chiTietPhieu.CauTraLoiText1 = txtKhac22.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }
                    //24
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 24);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr24.Text;
                        chiTietPhieu.CauTraLoiText1 = txtKhac24.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 24;
                        chiTietPhieu.CauTraLoiFIX1 = nmr24.Text;
                        chiTietPhieu.CauTraLoiText1 = txtKhac24.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }
                    //25
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 25);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr25.Text;
                        //chiTietPhieu.CauTraLoiText1 = txtKhac25.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 25;
                        chiTietPhieu.CauTraLoiFIX1 = nmr25.Text;
                        //  chiTietPhieu.CauTraLoiText1 = txtKhac25.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }
                    //26
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 26);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr26.Text;
                        chiTietPhieu.CauTraLoiText1 = txtKhac26.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 26;
                        chiTietPhieu.CauTraLoiFIX1 = nmr26.Text;
                        chiTietPhieu.CauTraLoiText1 = txtKhac26.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }

                    //27
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 27);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr27.Text;
                        //chiTietPhieu.CauTraLoiText1 = txtKhac25.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 27;
                        chiTietPhieu.CauTraLoiFIX1 = nmr27.Text;
                        //  chiTietPhieu.CauTraLoiText1 = txtKhac25.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }

                    //28
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 28);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr28.Text;
                        //chiTietPhieu.CauTraLoiText1 = txtKhac25.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 28;
                        chiTietPhieu.CauTraLoiFIX1 = nmr28.Text;
                        //  chiTietPhieu.CauTraLoiText1 = txtKhac25.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }
                    //29
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 29);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr29.Text;
                        //chiTietPhieu.CauTraLoiText1 = txtKhac25.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 29;
                        chiTietPhieu.CauTraLoiFIX1 = nmr29.Text;
                        //  chiTietPhieu.CauTraLoiText1 = txtKhac25.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }

                    //30
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 30);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr30.Text;
                        //chiTietPhieu.CauTraLoiText1 = txtKhac25.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 30;
                        chiTietPhieu.CauTraLoiFIX1 = nmr30.Text;
                        //  chiTietPhieu.CauTraLoiText1 = txtKhac25.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }


                    //31
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 31);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr31.Text;
                        //chiTietPhieu.CauTraLoiText1 = txtKhac25.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 31;
                        chiTietPhieu.CauTraLoiFIX1 = nmr31.Text;
                        //  chiTietPhieu.CauTraLoiText1 = txtKhac25.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }

                    //32
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 32);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr32.Text;
                        //chiTietPhieu.CauTraLoiText1 = txtKhac25.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 32;
                        chiTietPhieu.CauTraLoiFIX1 = nmr32.Text;
                        //  chiTietPhieu.CauTraLoiText1 = txtKhac25.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }

                    //33
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 33);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr33.Text;
                        chiTietPhieu.CauTraLoiText1 = txtKhac33.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 33;
                        chiTietPhieu.CauTraLoiFIX1 = nmr33.Text;
                        chiTietPhieu.CauTraLoiText1 = txtKhac33.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }

                    //34
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 34);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr34.Text;
                        chiTietPhieu.CauTraLoiText1 = txtKhac34.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 34;
                        chiTietPhieu.CauTraLoiFIX1 = nmr34.Text;
                        chiTietPhieu.CauTraLoiText1 = txtKhac34.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }


                    //35
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 35);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr35.Text;
                        //chiTietPhieu.CauTraLoiText1 = txtKhac35.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 35;
                        chiTietPhieu.CauTraLoiFIX1 = nmr35.Text;
                        //chiTietPhieu.CauTraLoiText1 = txtKhac34.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }

                    //36
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 36);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr36.Text;
                        //chiTietPhieu.CauTraLoiText1 = txtKhac35.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 36;
                        chiTietPhieu.CauTraLoiFIX1 = nmr36.Text;
                        //chiTietPhieu.CauTraLoiText1 = txtKhac34.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }

                    //37
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 37);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr37.Text;
                        //chiTietPhieu.CauTraLoiText1 = txtKhac35.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 37;
                        chiTietPhieu.CauTraLoiFIX1 = nmr37.Text;
                        //chiTietPhieu.CauTraLoiText1 = txtKhac34.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }

                    //38
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 38);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr38.Text;
                        //chiTietPhieu.CauTraLoiText1 = txtKhac35.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 38;
                        chiTietPhieu.CauTraLoiFIX1 = nmr38.Text;
                        //chiTietPhieu.CauTraLoiText1 = txtKhac34.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }

                    //39
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 39);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr39.Text;
                        //chiTietPhieu.CauTraLoiText1 = txtKhac35.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 39;
                        chiTietPhieu.CauTraLoiFIX1 = nmr39.Text;
                        //chiTietPhieu.CauTraLoiText1 = txtKhac34.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }

                    //40
                    chiTietPhieu = _repChiTietPhieu.GetByPhieuID_MaCau(_PhieuA.ID, 40);
                    if (chiTietPhieu != null)
                    {
                        chiTietPhieu.CauTraLoiFIX1 = nmr40.Text;
                        chiTietPhieu.CauTraLoiText1 = txtKhac40.Text;
                        _repChiTietPhieu.EditOne(chiTietPhieu);
                    }
                    else
                    {
                        chiTietPhieu = new BHXH.LinqRepository.ChiTietPhieu();
                        chiTietPhieu.PhieuID = phieuA.ID;
                        chiTietPhieu.MaCau = 40;
                        chiTietPhieu.CauTraLoiFIX1 = nmr40.Text;
                        chiTietPhieu.CauTraLoiText1 = txtKhac40.Text;
                        _repChiTietPhieu.InsertOne(chiTietPhieu);
                    }

                    _repChiTietPhieu.SubmitChanges();
                    #endregion

                    #region Kiến nghị
                    var kiennghi = _repKienNghi.GetByPhieuID_MaCau_MaCap1_MaCap2(_PhieuA.ID, 41, "1", "a");
                    if (kiennghi != null)
                    {
                        kiennghi.Text = txtKienNghiA41_1_a.Text;
                        _repKienNghi.EditOne(kiennghi);
                    }
                    else
                    {
                        kiennghi = new BHXH.LinqRepository.KienNghi();
                        kiennghi.PhieuID = phieuA.ID;
                        kiennghi.MaCau = 41;
                        kiennghi.MaCap1 = "1";
                        kiennghi.MaCap2 = "a";
                        kiennghi.Text = txtKienNghiA41_1_a.Text;
                        _repKienNghi.InsertOne(kiennghi);
                    }

                    kiennghi = _repKienNghi.GetByPhieuID_MaCau_MaCap1_MaCap2(_PhieuA.ID, 41, "1", "b");
                    if (kiennghi != null)
                    {
                        kiennghi.Text = txtKienNghiA40_1_b.Text;
                        _repKienNghi.EditOne(kiennghi);
                    }
                    else
                    {
                        kiennghi = new BHXH.LinqRepository.KienNghi();
                        kiennghi.PhieuID = phieuA.ID;
                        kiennghi.MaCau = 41;
                        kiennghi.MaCap1 = "1";
                        kiennghi.MaCap2 = "b";
                        kiennghi.Text = txtKienNghiA40_1_b.Text;
                        _repKienNghi.InsertOne(kiennghi);
                    }

                    kiennghi = _repKienNghi.GetByPhieuID_MaCau_MaCap1_MaCap2(_PhieuA.ID, 41, "1", "c");
                    if (kiennghi != null)
                    {
                        kiennghi.Text = txtKienNghiA40_1_c.Text;
                        _repKienNghi.EditOne(kiennghi);
                    }
                    else
                    {
                        kiennghi = new BHXH.LinqRepository.KienNghi();
                        kiennghi.PhieuID = phieuA.ID;
                        kiennghi.MaCau = 41;
                        kiennghi.MaCap1 = "1";
                        kiennghi.MaCap2 = "c";
                        kiennghi.Text = txtKienNghiA40_1_c.Text;
                        _repKienNghi.InsertOne(kiennghi);
                    }

                    kiennghi = _repKienNghi.GetByPhieuID_MaCau_MaCap1_MaCap2(_PhieuA.ID, 41, "1", "d");
                    if (kiennghi != null)
                    {
                        kiennghi.Text = txtKienNghiA40_1_d.Text;
                        _repKienNghi.EditOne(kiennghi);
                    }
                    else
                    {
                        kiennghi = new BHXH.LinqRepository.KienNghi();
                        kiennghi.PhieuID = phieuA.ID;
                        kiennghi.MaCau = 41;
                        kiennghi.MaCap1 = "1";
                        kiennghi.MaCap2 = "d";
                        kiennghi.Text = txtKienNghiA40_1_d.Text;
                        _repKienNghi.InsertOne(kiennghi);
                    }


                    kiennghi = _repKienNghi.GetByPhieuID_MaCau_MaCap1_MaCap2(_PhieuA.ID, 41, "1", "e");
                    if (kiennghi != null)
                    {
                        kiennghi.Text = txtKienNghiA40_1_e.Text;
                        _repKienNghi.EditOne(kiennghi);
                    }
                    else
                    {
                        kiennghi = new BHXH.LinqRepository.KienNghi();
                        kiennghi.PhieuID = phieuA.ID;
                        kiennghi.MaCau = 41;
                        kiennghi.MaCap1 = "1";
                        kiennghi.MaCap2 = "e";
                        kiennghi.Text = txtKienNghiA40_1_e.Text;
                        _repKienNghi.InsertOne(kiennghi);
                    }

                    kiennghi = _repKienNghi.GetByPhieuID_MaCau_MaCap1_MaCap2(_PhieuA.ID, 41, "2", "");
                    if (kiennghi != null)
                    {
                        kiennghi.Text = txtKienNghiA28_2.Text;
                        _repKienNghi.EditOne(kiennghi);
                    }
                    else
                    {
                        kiennghi = new BHXH.LinqRepository.KienNghi();
                        kiennghi.PhieuID = phieuA.ID;
                        kiennghi.MaCau = 41;
                        kiennghi.MaCap1 = "2";
                        kiennghi.MaCap2 = "";
                        kiennghi.Text = txtKienNghiA28_2.Text;
                        _repKienNghi.InsertOne(kiennghi);
                    }

                    kiennghi = _repKienNghi.GetByPhieuID_MaCau_MaCap1_MaCap2(_PhieuA.ID, 41, "3", "");
                    if (kiennghi != null)
                    {
                        kiennghi.Text = txtKienNghiA28_3.Text;
                        _repKienNghi.EditOne(kiennghi);
                    }
                    else
                    {
                        kiennghi = new BHXH.LinqRepository.KienNghi();
                        kiennghi.PhieuID = phieuA.ID;
                        kiennghi.MaCau = 41;
                        kiennghi.MaCap1 = "3";
                        kiennghi.MaCap2 = "";
                        kiennghi.Text = txtKienNghiA28_3.Text;
                        _repKienNghi.InsertOne(kiennghi);
                    }


                    #endregion
                    _repKienNghi.SubmitChanges();
                    SetEditState(EditState.Saved);
                    //transaction.Commit();
                    _SavedOK = true;
                }
                catch (Exception ex)
                {
                    //transaction.Rollback();
                    throw ex;
                }
                finally
                {
                    //con.Close();
                    txtMaPhieu.Focus();
                }
            }
            catch (Exception ex)
            {
                _SavedOK = false;
                BHXH.Repository.FrmExceptionNotifier.ShowAndLog(ex);
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            Edit();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            Save();
        }
        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void nmrB5_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmrB5.Text.Trim();
            SetCauTraLoi(nmrB5, "PhieuB", maTraLoi, 5, 0, tlB5);
            // SetCauTraLoi(nmrB5, "PhieuB", maTraLoi, 5, 0, tlB5
        }

        private void nmrB4_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmrB4.Text.Trim();
            SetCauTraLoi(nmrB4, "PhieuB", maTraLoi, 4, 0, tlB4);
        }

        private void nmrB7_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmrB7.Text.Trim();
            SetCauTraLoi(nmrB7, "PhieuB", maTraLoi, 7, 0, tlB7);
        }

        private void nmrB8_1_TextChanged(object sender, EventArgs e)
        {
            string maTraLoi = nmrB8_1.Text.Trim();
            SetCauTraLoi(nmrB8_1, "PhieuB", maTraLoi, 8, 1, tlB8_2);
        }

        string buidQueryPhieuB(int maCau)
        {
            string query = "";
            if (_PhieuA != null) query = string.Format("SELECT * FROM Phieu_DangBang WHERE PhieuID = '{0}' AND MaCau = {1} ORDER BY ThuTuTrongBang", _PhieuA.ID, maCau);
            return query;
        }
        /// <summary>
        /// Edite data
        /// </summary>
        protected override void Edit()
        {
            SetEditState(BHXH.Repository.EditState.Modifying);
            txtMaPhieu.Focus();
        }
        void LoadCauTraLoiDangBangPhieuB()
        {
            //
            BHXH.LinqRepository.Phieu_DangBang _item = new BHXH.LinqRepository.Phieu_DangBang();
            if (_PhieuA != null)
                lstSource = _repPhieu_dangBang.GetAllByPhieuId(_PhieuA.ID);
            source12 = new List<BHXH.LinqRepository.Phieu_DangBang>();
            _item = new BHXH.LinqRepository.Phieu_DangBang() { ThuTuTrongBang = 0, STT = "1", ChiTieu = "Số người lao động đã được cấp sổ BHXH", Number1 = GetSourceByMaCau(12, 1), Number2 = GetSourceByMaCau(12, 2), Number3 = null, Number4 = null };
            source12.Add(_item);
            _item = new BHXH.LinqRepository.Phieu_DangBang() { ThuTuTrongBang = 1, STT = "2", ChiTieu = "Số người lao động chưa được cấp sổ BHXH", Number1 = GetSourceByMaCau(12, 3), Number2 = GetSourceByMaCau(12, 4), Number3 = null, Number4 = null };
            source12.Add(_item);
            bind12.DataSource = source12;
            gridControlA_12.DataSource = bind12;
            gridControlA_12.RefreshDataSource();
        }

        decimal? GetSourceByMaCau(int maCau, int number)
        {
            if (lstSource == null) return null;
            if (number == 1)
                return lstSource.Where(c => c.MaCau == maCau && c.ThuTuTrongBang==0).Select(c => c.Number1).FirstOrDefault();
            else if (number == 2)
                return lstSource.Where(c => c.MaCau == maCau && c.ThuTuTrongBang == 0).Select(c => c.Number2).FirstOrDefault();
            else if (number == 3)
                return lstSource.Where(c => c.MaCau == maCau && c.ThuTuTrongBang == 1).Select(c => c.Number1).FirstOrDefault();
            else if (number == 4)
                return lstSource.Where(c => c.MaCau == maCau && c.ThuTuTrongBang == 1).Select(c => c.Number2).FirstOrDefault();

            return null;
        }

        void InsertOrUpdateToDB(BHXH.LinqRepository.Phieu phieu, List<BHXH.LinqRepository.Phieu_DangBang> source, int MaCau)
        {
            try
            {
                if (_EditState == BHXH.Repository.EditState.Modifying)
                {
                    //var objDeleteDangBang = source.Where(c => c.PhieuID == phieu.ID).ToList();
                    _repPhieu_dangBang.DeleteAllByPhieuIdMaCau(phieu.ID, MaCau);
                }
                //
                List<BHXH.LinqRepository.Phieu_DangBang> lst = new List<BHXH.LinqRepository.Phieu_DangBang>();
                BHXH.LinqRepository.Phieu_DangBang phieu_dangBang = new BHXH.LinqRepository.Phieu_DangBang();
                foreach (BHXH.LinqRepository.Phieu_DangBang item in source)
                {
                    phieu_dangBang = new BHXH.LinqRepository.Phieu_DangBang();
                    phieu_dangBang.ID = Guid.NewGuid();
                    phieu_dangBang.MaCau = MaCau;
                    phieu_dangBang.PhieuID = phieu.ID;
                    phieu_dangBang.Number1 = item.Number1;
                    phieu_dangBang.Number2 = item.Number2;
                    phieu_dangBang.Number3 = item.Number3;
                    phieu_dangBang.Number4 = item.Number4;
                    phieu_dangBang.ThuTuTrongBang = item.ThuTuTrongBang;
                    lst.Add(phieu_dangBang);
                }
                _repPhieu_dangBang.InsertAll(lst);
                _repPhieu_dangBang.SubmitChanges();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        private void gridViewA_12_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Value == null)
            {
                MessageBox.Show("Cột dữ liệu không để trống", "Thông báo");
                gridViewA_12.FocusedColumn = e.Column;
            }

        }

        private void txtKienNghiA28_3_TextChanged(object sender, EventArgs e)
        {
            Save();
        }




    }
}
